package forms;

import helpers.CrawlerHelper;
import helpers.LanguageHelper;
import helpers.StringHelper;
import helpers.TranslateHelper;
import helpers.ValidateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import models.ArticleModel;

import entities.Article;

public class ArticleForm {
	
	private static final Logger log = Logger.getLogger(ArticleForm.class.getName());
	
	public static TreeMap checkValid(HttpServletRequest request, Article article) {
		TreeMap form = new TreeMap();
		TreeMap<String, String> checkValid = new TreeMap<String, String>();
		
		String published = (String) request.getParameter("published");
		String title = (String) request.getParameter("title");
		String content = (String) request.getParameter("content");
		String category = (String) request.getParameter("category");
		String name = (String) request.getParameter("name");
		String email = (String) request.getParameter("email");
		String website = (String) request.getParameter("website");
		String keywords = (String) request.getParameter("keywords");
		
		
		if (article.getCreator().isEmpty()) {
			if (name == null || name.isEmpty()) {
				checkValid.put("name", LanguageHelper.getString("article_name_error"));
				article.setAuthorName("");
			} else {
				article.setAuthorName(name);
			}
			
			if (email == null || email.isEmpty()) {
				checkValid.put("email", LanguageHelper.getString("article_email_error_input"));
				article.setEmail("");
			} else if (!ValidateHelper.isEmail(email)) {
				checkValid.put("email", LanguageHelper.getString("article_email_error_valid"));
				article.setEmail(email);
			} else {	
				article.setEmail(email);
			}
			
			if (website != null) {
				article.setWebsite(website);
			}
		}
		
		//Set old alias
		article.setOldAlias(article.getAlias());
		
		if (title == null || title.isEmpty()) {
			checkValid.put("title", LanguageHelper.getString("article_title_error_input"));
			article.setTitle("");
		} else {
			article.setTitle(title);
		}
		
		if (title != null) {
			String alias = StringHelper.replace(title);
			ArticleModel newsModel = ArticleModel.getInstance();
			if (!alias.isEmpty() && alias.equalsIgnoreCase(article.getAlias())) {
				article.setAlias(alias);
			} else if (alias.isEmpty() || newsModel.checkAliasExists(alias, article.getLanguage())) {
				checkValid.put("title", LanguageHelper.getString("article_title_error_exists"));
				article.setAlias(alias);
			} else {
				article.setAlias(alias);
			}
		}
		
		if (content == null || content.isEmpty()) {
			checkValid.put("content", LanguageHelper.getString("article_content_error_input"));
			article.setContent("");
		} else {
			Document doc = Jsoup.parse(content);
			Elements elms = doc.select("a");
			if (elms.size() > 5) {
				checkValid.put("content", "Maximum link: 5");
			}
			article.setContent(content);
		}
		
		if (category == null || category.isEmpty()) {
			checkValid.put("category", LanguageHelper.getString("article_category_error_input"));
			article.setCategory("");
		} else {
			article.setCategory(category);
		}
		
		if (keywords != null && !keywords.isEmpty()) {
			String[] k = keywords.split(",");
			List<String> keys = new ArrayList<String>();
			for (int i = 0; i < k.length; i++) {
				keys.add(k[i].trim());
			}
			
			if (k.length > 5) {
				checkValid.put("keywords", "Maximum: 5 keywords");
			}
			
			
			article.setKeywords(keys);
			article.setTags(keywords);
		}
		
		form.put("checkValid", checkValid);
		form.put("article", article);
		
		return form;
	}
	
	public static TreeMap checkValidSubmitService(HttpServletRequest request, Article article) {
		TreeMap form = new TreeMap();
		TreeMap checkValid = new TreeMap();
		
		String title = (String) request.getParameter("post[title]");
		String content = (String) request.getParameter("post[content]");
		String category = (String) request.getParameter("post[category]");
		String name = (String) request.getParameter("post[name]");
		String email = (String) request.getParameter("post[email]");
		String website = (String) request.getParameter("post[website]");
		String keywords = (String) request.getParameter("post[keywords]");
		String sl = (String) request.getParameter("post[sl]");
		String tl = (String) request.getParameter("post[tl]");
		
		
		if (article.getCreator().isEmpty()) {
			if (name != null) {
				article.setAuthorName(name);
			}
			
			if (email != null) {
				article.setEmail(email);
			}
			
			if (website != null) {
				article.setWebsite(website);
			}
		}
		
		//Set old alias
		article.setOldAlias(article.getAlias());
		
		if (title == null || title.isEmpty()) {
			checkValid.put("title", LanguageHelper.getString("article_title_error_input"));
			article.setTitle("");
		} else {
			if (sl != null && !sl.isEmpty() && tl != null && !tl.isEmpty()) {
				try {
					article.setTitle(TranslateHelper.translate(title, sl, tl));
				} catch (Exception e) {
					article.setTitle(title);
				}
				
			} else {
				article.setTitle(title);
			}
			
		}
		
		if (title != null) {
			String alias = StringHelper.replace(title);
			ArticleModel newsModel = ArticleModel.getInstance();
			if (!alias.isEmpty() && alias.equalsIgnoreCase(article.getAlias())) {
				article.setAlias(alias);
			} else if (alias.isEmpty() || newsModel.checkAliasExists(alias, article.getLanguage())) {
				checkValid.put("title", LanguageHelper.getString("article_title_error_exists"));
				article.setAlias(alias);
			} else {
				article.setAlias(alias);
			}
		}
		
		if (content == null || content.isEmpty()) {
			checkValid.put("content", LanguageHelper.getString("article_content_error_input"));
			article.setContent("");
		} else {
			Document doc = Jsoup.parse(content);
			Elements elms = doc.select("a");
			if (elms.size() > 5) {
				checkValid.put("content", "Maximum link: 5");
			}
			
			if (sl != null && !sl.isEmpty() && tl != null && !tl.isEmpty()) {
				try {
					article.setContent(TranslateHelper.translate(content, sl, tl));
				} catch (Exception e) {
					article.setContent(content);
				}
			} else {
				article.setContent(content);
			}
		}
		
		if (category == null || category.isEmpty()) {
			checkValid.put("category", LanguageHelper.getString("article_category_error_input"));
			article.setCategory("");
		} else {
			article.setCategory(category);
		}
		
		if (keywords == null || keywords.isEmpty()) {
			String tags = CrawlerHelper.getTag(StringHelper.html2text(article.getContent()));
			List<String> keys = new ArrayList<String>();
			if (!tags.isEmpty()) {
				String[] k = tags.split(",");
				keys = new ArrayList<String>();
				for (int i = 0; i < k.length; i++) {
					if (!k[i].isEmpty() && k[i].length() > 2) {
						keys.add(k[i].trim());
					}
				}
			}
			article.setTags(tags);
			article.setKeywords(keys);
			
		}else if (keywords != null && !keywords.isEmpty()) {
			String[] k = keywords.split(",");
			List<String> keys = new ArrayList<String>();
			for (int i = 0; i < k.length; i++) {
				keys.add(k[i].trim());
			}
			
			if (k.length > 5) {
				checkValid.put("keywords", "Maximum: 5 keywords");
			}
			
			
			article.setKeywords(keys);
			article.setTags(keywords);
		}
		
		if (tl != null && !tl.isEmpty()) {
			article.setLanguage(tl);
		}
		
		form.put("checkValid", checkValid);
		form.put("article", article);
		
		return form;
	}
}
